/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */

package com.simon.mengkou.ui.view;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * @author : Zhenshui.Xia
 * @date   : 2014-3-10
 * @desc   : 
 */
public class LoopPageIndicator extends LinearLayout{
	
	private Context mContext;
    
    //其他页面标识
    private Drawable mNormalDrawable;
    //当前页面标识
    private Drawable mFocusDrawable;
    
    //总页面数
    private int mTotalPage=0;
    //当前页面
    private int mCurPageNum=0;

    

	public LoopPageIndicator(Context context) {
		super(context);
		this.mContext = context;
	}
	
	public LoopPageIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
	}
	
	public void initIndicator(int curPage, int totalPage, int normalDrawable, int focusDrawable) {
		this.mTotalPage = totalPage;
		this.mNormalDrawable = getResources().getDrawable(normalDrawable);
		this.mFocusDrawable = getResources().getDrawable(focusDrawable);
		refreshIndicator(curPage);
	}
	
	
	public void refreshIndicator(int curPage) {
        mCurPageNum = curPage;
        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER);
        //清空容器的view对象
        removeAllViews();
        
        //添加分页指导器
        for(int i = 0; i<mTotalPage; i++) {
        	//view的属性设置
            LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin=3;
            layoutParams.rightMargin=3;
          
            //新建ImageView，用来显示分页图标
            ImageView newPageImageView = new ImageView(mContext);
            
            //按照当前页还是其他页面设置对应图标
            if(i == mCurPageNum){
                newPageImageView.setBackgroundDrawable(mFocusDrawable);
            } else {
                newPageImageView.setBackgroundDrawable(mNormalDrawable);
            }
            
           
            //把图标添加到LinearLayout
            addView(newPageImageView, layoutParams);              
        } 
    }

}
